<?php

namespace App\Http\Controllers\Vote;

use App\Http\Middleware\SurveyCateMiddleware;
use App\Model\Survey;
use App\Model\SurveyColleague;
use App\Model\SurveyMemberAnswer;
use App\Model\SurveyQuestion;
use App\Model\SurveyQuestionOption;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cookie;

class StaffController extends Controller
{
    public function __construct()
    {
        $this->middleware('cate', ['except' => ['submit']]);
    }


    //员工满意度调查引导页
    public function index($id)
    {
        $survey = Survey::find($id);
        return view('vote.index2', [
            'survey' => $survey
        ]);
    }

    /**
     * 问卷调查页
     */
    public function survey($id)
    {
//        $id = 13;    //问卷id
        //验证有没有填过该问卷
//        $cookie = request()->cookie('survey_'.$id);
//        if($cookie){
//            return redirect()->back()->withErrors(['submit' => '您已经填写过这个调查了']);
//        }

        $department = request('department');

        if(!$department){
            return redirect()->back()->withErrors(['submit' => '请选择您的部门']);
        }

        $survey = Survey::find($id);
        $questions = SurveyQuestion::where('survey_id', $id)->get();
        $question_ids = array_map(function($a){ return $a['id']; },$questions->toArray());
        $options = SurveyQuestionOption::whereIn('question_id', $question_ids)->get();
//        dd($question_ids);
        return view('vote.survey',[
            'survey' => $survey,
            'questions' => $questions,
            'options' => $options,
            'department' => $department
        ]);
    }

    /**
     * 提交问卷
     */
    public function submit()
    {
        $post = request()->all();

//        dd($post);

        //验证有没有填过该问卷
//        $cookie = request()->cookie('survey_'.$post['survey_id']);
//        if($cookie){
//            return redirect('staff/index')->withErrors(['submit' => '您已经填写过这个调查了']);
//        }



        //现将用户信息保存到 colleagues 表
        $survey_member_id = SurveyColleague::insertGetId([
            'dpt' => $post['department'],
            'survey_id' => $post['survey_id'],
            'ip' => request()->ip(),
        ]);

        //添加数据到用户答案表
        //重组用户答案数据
        $options = $post['option'];

        $addData = [];
        foreach($options as $k => $v){
            if(is_array($v)){
                foreach($v as $option){
                    $tmp['survey_member_id'] = $survey_member_id;
                    $tmp['survey_id'] = $post['survey_id'];
                    $tmp['question_id'] = $k;
                    $split = explode('_',$option);
                    $tmp['option_id'] = $split[0];
                    $tmp['answer_content'] = $split[1];

                    $addData[] = $tmp;
                }
            }else{
                $tmp['survey_member_id'] = $survey_member_id;
                $tmp['survey_id'] = $post['survey_id'];
                $tmp['question_id'] = $k;

                $split = explode('_',$v);

                if(is_numeric($split[0]) && count($split) == 2){

                    $tmp['option_id'] = $split[0];
                    $tmp['answer_content'] = $split[1];
                }else{

                    $tmp['option_id'] = 0;
                    $tmp['answer_content'] = $v;
                }


                $addData[] = $tmp;
            }
        }

        //将数据添加到数据库
        SurveyMemberAnswer::insert($addData);
        //添加cookie到客户端
        $cookie = Cookie::forever('survey_'.$post['survey_id'] , 'yes');

        $view = view('vote.submit');
        return response($view)->withCookie($cookie);
//        return $view;
    }
}
